/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.Modeling.Validation;

namespace DslEditorPowerToy.Languages.EditorConfiguration
{
    #region Calculated Fields
    partial class DslEditor
    {
        /// <summary>
        /// Return the calculated class name
        /// </summary>
        /// <returns></returns>
        private string GetClassNameValue()
        {
            return string.Format("Editor_{0}", this.Id.ToString("N"));
        }
        /// <summary>
        /// Return the calculated identifier
        /// </summary>
        /// <returns></returns>
        private string GetEditorIdValue()
        {
            return this.Id.ToString("D");
        }    
    }
    #endregion

    #region Validation
    [ValidationState(ValidationState.Enabled)]
    partial class DslEditor
    {
        #region Validation Methods
        [ValidationMethod(ValidationCategories.Save | ValidationCategories.Menu | ValidationCategories.Open)]
        internal void ValidateCaption(ValidationContext context)
        {
            //Verify caption is specified
            if (true == string.IsNullOrEmpty(this.Caption))
                context.LogError(Resources.EditorCaptionUndefined, Resources.Class_EditorCaptionUndefined, this);
        }
        [ValidationMethod(ValidationCategories.Save | ValidationCategories.Menu | ValidationCategories.Open)]
        internal void ValidateEditorControl(ValidationContext context)
        {
            //Verify control is specified
            if (true == string.IsNullOrEmpty(this.EditorControl))
            {
                context.LogError(string.Format(Resources.EditorControlUndefined, this.Caption),
                    Resources.Class_EditorControlUndefined, this);
                return;
            }

            ////Verify type is valid (This test only works if in currently loaded assemblies)
            //Type controlType = Type.GetType(this.EditorControl, false, true);
            //if ((null == controlType)
            //    || (false == controlType.IsSubclassOf(typeof(System.Windows.Forms.Control))))
            //    context.LogError(string.Format(Resources.ViewEditorControlInvalidType, this.Caption),
            //        Resources.Class_ViewEditorControlInvalidType, this);
        }
        [ValidationMethod(ValidationCategories.Save | ValidationCategories.Menu | ValidationCategories.Open)]
        internal void ValidateIcon(ValidationContext context)
        {
            //Verify icon is defined if specified
            if (false == string.IsNullOrEmpty(this.Icon))
            {
                if (false == File.Exists(this.Icon))
                    context.LogError(string.Format(Resources.EditorIconNotExist, this.Caption),
                        Resources.Class_EditorIconNotExist, this);
            }
        }
        #endregion
    }
        #endregion

    #region Property Display
    [TypeDescriptionProvider(typeof(EditorConfigurationDescriptionProvider))]
    partial class DslEditor
    {
    }
    #endregion
}
